package com.dayu.gulimall.controller;

import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dayu
 * @version 1.0
 * @email dayu007@foxmail.com
 * @date 2021/3/21 11:18
 * @description
 */
@Controller
public class LoginController {

    @Value("${ssoserver.url}")
    private String SSOServerUrl;

    @Autowired
    StringRedisTemplate redisTemplate;

    @ResponseBody
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/boss")
    public String emps(Model model, HttpSession session, @RequestParam(value = "token",required = false) String token) {
        if (!StringUtils.isEmpty(token)){
            String username = redisTemplate.opsForValue().get(token);
            session.setAttribute("loginUser",username);
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            System.out.println("未登录账号0");
            return "redirect:"+SSOServerUrl+"?redirect_url=http://client1.com:8082/boss";
        } else {
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            model.addAttribute("emps", emps);
            return "list";
        }
    }


}
